/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.util;

import java.util.Random;

/**
 * int值范围.
 * 
 * @version 2014年3月16日下午2:13:11
 * @author jrwz
 */
public final class RangeInt {
    private static final Random rnd = new Random();
    /** 低. */
    private final int low;
    /** 高. */
    private final int high;

    public RangeInt(final int low, final int high) {
        this.low = low;
        this.high = high;
    }

    public RangeInt(final RangeInt range) {
        this(range.low, range.high);
    }

    /**
     * 是否在范围内
     * 
     * @param i
     *            数值
     * @return 如果在则返回true
     */
    public boolean includes(final int i) {
        return (low <= i) && (i <= high);
    }

    public static boolean includes(final int i, final int low, final int high) {
        return (low <= i) && (i <= high);
    }

    /**
     * 四舍五入.
     * 
     * @param i
     *            数值
     * @return 结果
     */
    public int ensure(final int i) {
        int r = i;
        r = (low <= r) ? r : low;
        r = (r <= high) ? r : high;
        return r;
    }

    /**
     * 确保值在合法范围内.
     * 
     * @param n
     *            - 给值
     * @param low
     *            - 最小值
     * @param high
     *            - 最大值
     * @return 合适的值(最大不会超过high、最小不会低于low)
     */
    public static int ensure(final int n, final int low, final int high) {
        int r = n;
        r = (low <= r) ? r : low;
        r = (r <= high) ? r : high;
        return r;
    }

    /**
     * 生成随机值.
     * 
     * @return 范围内的随机值
     */
    public int randomValue() {
        return rnd.nextInt(getWidth() + 1) + low;
    }

    /**
     * 取得低.
     * 
     * @return 低
     */
    public int getLow() {
        return low;
    }

    /**
     * 取得高.
     * 
     * @return 高
     */
    public int getHigh() {
        return high;
    }

    /**
     * 取得宽.
     * 
     * @return 宽
     */
    public int getWidth() {
        return high - low;
    }

    @Override
    public boolean equals(final Object obj) {
        if (!(obj instanceof RangeInt)) {
            return false;
        }
        final RangeInt range = (RangeInt) obj;
        return (low == range.low) && (high == range.high);
    }

    @Override
    public String toString() {
        return "low=" + low + ", high=" + high;
    }
}
